import Taro from '@tarojs/taro'
import { useState, useEffect } from 'react'

export function useAuth() {
  const [isLoggedIn, setIsLoggedIn] = useState(false)
  const [userName, setUserName] = useState('')

  useEffect(() => {
    const loginState = Taro.getStorageSync('isLoggedIn')
    console.log('useAuth: loginState:', loginState)
    const name = Taro.getStorageSync('userName')
    setIsLoggedIn(!!loginState)
    setUserName(name || '')
  }, [])

  const login = (name = '赵四') => {
    setIsLoggedIn(true)
    setUserName(name)
    Taro.setStorageSync('isLoggedIn', true)
    Taro.setStorageSync('userName', name)
  }

  const logout = () => {
    setIsLoggedIn(false)
    setUserName('')
    Taro.setStorageSync('isLoggedIn', false)
    Taro.setStorageSync('userName', '')
  }

  return { isLoggedIn, userName, login, logout }
} 